﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class PlayerControl : MonoBehaviour {



    private float _runSpeedY = 10f;
    private float _runSpeedX = 3f;

    private Vector3 _rota = new Vector3(0, 0, 0);

    [SerializeField]
    private float sensitivityx = 2f;
    [SerializeField]
    private float sensitivityy = 2f;

    private Vector3 Up = Vector3.up;
    private Vector3 Right;
    public  GameObject effcetSpeed;
    //视角范围
    private float miny = -60f;
    private float maxy = 60f;
    private float rotationy = 0f;
    public Player _player;
    private void Start()
    {
        Up = Vector3.up;
        Right = transform.right;
    }

    private void Update()
    {
        print(_player.IsPlayerAlive);
        if (_player.IsPlayerAlive)
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");

            if (v > 0)
            {
                transform.Translate(Vector3.forward * _runSpeedY * Time.deltaTime);
              //  effcetSpeed.SetActive(true);
            }
            else if (v < 0)
            {
                transform.Translate(-Vector3.forward * _runSpeedY * Time.deltaTime);
              //  effcetSpeed.SetActive(true);
            }
           else if(v==0)
            {
              //  effcetSpeed.SetActive(false);
            }
            if (h > 0)
            {
                transform.Translate(Vector3.right * _runSpeedX * Time.deltaTime);
            }
            else if (h < 0)
            {
                transform.Translate(Vector3.left * _runSpeedX * Time.deltaTime);

            }
           
                float rotationx = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityx;
                transform.rotation = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * sensitivityx, Up) * transform.rotation;
                transform.rotation = Quaternion.AngleAxis(-Input.GetAxis("Mouse Y") * sensitivityx, Right) * transform.rotation;
                Right = transform.right;
                //设置相机角度
                // transform.localEulerAngles = new Vector3(rotationx_X, rotationx, 0);
            
            //控制相机左右旋转 



            RotationCamera();
            Distance();
        }
    } 
    /// <summary>
    /// 按space 旋转摄像机
    /// </summary>
    public void RotationCamera()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            RaycastHit hit;
            Physics.Raycast(transform.position, transform.forward, out hit, 10);
            if (hit.transform != null)
            {
                Vector3 target = hit.point +(transform.position - hit.point)*0.5f;
                Debug.Log("hit");
                transform.DOMove(target, 0.1f);
            }
            transform.rotation = Quaternion.AngleAxis(-90f, Right) *transform.rotation  ;
            Up = Quaternion.AngleAxis(-90f, Right) * Up;
            Physics.gravity = -Up;

        }
    }
   
   /// <summary>
   /// 画辅助线的
   /// </summary>
    void OnDrawGizmos()
    {
        Gizmos.DrawLine(transform.position, transform.position + Up);
    }

   /// <summary>
   /// 计算距离判断死亡
   /// </summary>
    private void Distance()
    {
        GameObject go = GameObject.Find("model");
        if (Vector3.Distance(this.transform.position, go.transform.position)>200f)
        {
            _player.IsPlayerAlive = false;
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag=="die")
        {
            _player.IsPlayerAlive = false;
            UIManager.Instance.ShowFailedPanel();
        }
    }
}
